//
//  HomeViewController.m
//  My1A2B
//
//  Created by sun ives on 2009/12/28.
//  Copyright __MyCompanyName__ 2009. All rights reserved.
//

#import "HomeViewController.h"
#import "PlayViewController.h"
#import "My1A2BAppDelegate.h"
#import "define_tw.h"
//#import "Core.h"

@implementation HomeViewController
@synthesize bThree, bFour, bProcess, m_appDelegate, m_answerCount;


// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
	self.m_appDelegate = [[UIApplication sharedApplication] delegate];
    return self;
}



// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
	[super loadView];
	self.view.backgroundColor = [UIColor whiteColor];
	NSString *title;
	title = [[NSString alloc] initWithFormat:@"%@", THREE]; 
	[bThree setTitle:title forState:UIControlStateNormal];
	[title release];
	title = [[NSString alloc] initWithFormat:@"%@", FOUR]; 
	[bFour setTitle:title forState:UIControlStateNormal];
	[title release];
}



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	self.title = VIEW_HOME;
	
	NSArray *segmentTextContent = [NSArray arrayWithObjects:
								   NSLocalizedString(THREE, @""),
								   NSLocalizedString(FOUR, @""),
								   nil];
	UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentTextContent];
	//segmentedControl.selectedSegmentIndex = 0;
	//segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
	segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
	segmentedControl.frame = CGRectMake(50, 0, 200, 30.0);
	[segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
	
	//defaultTintColor = [segmentedControl.tintColor retain];	// keep track of this for later
	
	self.navigationItem.titleView = segmentedControl;
	[segmentedControl release];
	
	
    UIBarButtonItem *addButtonItem = [[UIBarButtonItem alloc] initWithTitle:VIEW_HISTORY style:UIBarButtonItemStylePlain target:self action:@selector(frontHistory:)];
    self.navigationItem.leftBarButtonItem = addButtonItem;
    [addButtonItem release];
}



/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)frontHistory:(id)sender{
	if ([m_appDelegate.core isNewRecord]){
		[m_appDelegate.historyTableViewController reloadData];
		[m_appDelegate.core setNewRecordFalse];
	}
	[self.navigationController pushViewController:m_appDelegate.historyTableViewController animated:YES];
}

- (IBAction)frontPlay:(UIButton *)sender{
	NSString *title = sender.currentTitle;
	if ([title isEqualToString:THREE]) {
		m_answerCount = 3;
	}else if ([title isEqualToString:FOUR]) {
		m_answerCount = 4;
	}
	if ([m_appDelegate.core getAnswer] != nil) {
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"訊息" message:@"您還有遊戲在進行\n確定要建立新遊戲嗎？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];
		[alert show];
		[alert release];
	}else{
		[m_appDelegate.core reStart:m_answerCount];		
		[self newGame];
	}

}
- (void)newGame{
	m_appDelegate.playViewController.m_answerCount = [m_appDelegate.core getAnswerCount];
	[self.navigationController pushViewController:m_appDelegate.playViewController animated:YES];
	UIScrollView *scrollView = (UIScrollView *)m_appDelegate.playViewController.view;
	scrollView.scrollEnabled = YES;
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
	switch (buttonIndex) {
		case 0:
			if (m_appDelegate.playViewController != nil) {
				[self.navigationController pushViewController:m_appDelegate.playViewController animated:YES];
			}
			break;
		case 1:
			[m_appDelegate.core reStart:m_answerCount];
			[m_appDelegate.playViewController loadView];
			[self newGame];
			break;
		default:
			break;
	}
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

@end
